package creational_pattern.prototype_pattern;

import java.io.*;

public class DeepClone {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        WeeklyLog1 weeklyLog1 = new WeeklyLog1();
        weeklyLog1.setAttachment(new Attachment());
        weeklyLog1.setContent("content");
        WeeklyLog1 weeklyLog11 = weeklyLog1.deepClone();
        System.out.println(weeklyLog1 == weeklyLog11);
        System.out.println(weeklyLog1.getAttachment() == weeklyLog11.getAttachment());

    }
}
class WeeklyLog1 implements Serializable{
    private Attachment attachment;
    private String name;
    private String content;

    public Attachment getAttachment() {
        return attachment;
    }

    public void setAttachment(Attachment attachment) {
        this.attachment = attachment;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
    public WeeklyLog1 deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bao);
        oos.writeObject(this);
        ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (WeeklyLog1) ois.readObject();
    }
}
class Attachment implements Serializable{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void download(){
        System.out.println("download attachment "+this.name);
    }
}
